package io.renren.modules.dishes.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.dishes.entity.SpuVo;
import io.renren.modules.dishes.service.impl.ClassficationServiceImpl;
import io.renren.modules.dishes.service.impl.spuServiceImpl;
import io.renren.modules.merchant.service.impl.MerchantServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

@RestController
@RequestMapping("/web/spuController")
public class spuController {

    @Autowired
    private spuServiceImpl spuService;
    @Autowired
    private MerchantServiceImpl merchantService;



    @GetMapping("/list")
    public R getList(@RequestParam Map<String, Object> params){
        Long userId = getUserId();
        String merchantId = merchantService.getMerchantByUserId(userId).getMerchantId();
        params.put("merchantId",merchantId);
        PageUtils page = spuService.queryPage(params);
        return R.ok().put("page", page);
    }

    @PostMapping("/save")
    public R save(@RequestBody SpuVo spuVo) throws JsonProcessingException {
        spuService.saveSpuVO(spuVo);
        return R.ok();
    }

    @GetMapping("/get/{spuId}")
    public R GetInfo(@PathVariable("spuId") String spuId) throws IOException {
        SpuVo vo = spuService.getVO(spuId);
        return R.ok().put("vo",vo);
    }

    @PostMapping("/update")
    public R update(@RequestBody SpuVo spuVo) throws JsonProcessingException {
        spuService.updateSpuVO(spuVo);
        return R.ok();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody String[] spuIds){
        Integer integer = spuService.deleteBatch(spuIds);
        return R.ok().put("number",integer);
    }



}
